const arr = [
    { name: "张三", age: 20 },
    { name: "李四", age: 22 },
    { name: "王五", age: 21 },
  ];
  
  arr.forEach((item, index) => {
    console.log(index);
    console.log(item);
  });
  
  const findFirst = arr.find((item) => {
    return item.age > 20;
  });
  
  console.log("\n大于20岁", findFirst);
  
  const findAll = arr.filter((item) => {
    return item.age > 20;
  });
  
  console.log("\n", findAll);
  
  //映射:elmeA->elemB 生成新数组
  //返回新的对象,且年龄都加1
  
  const mapArr = arr.map(function (item) {
    // const newItem={};
    // newItem.name=item.name;
    // newItem.age=item.age+1;
    const newItem = { ...item, age: item.age + 1 };
    return newItem;
  });
  
  console.log(mapArr);
  console.log(mapArr === arr);
  
  //排序,不生成新数组
  const sortArr=arr.sort((a, b) => {
    return a.age - b.age;
  });
  console.log(sortArr);
  console.log(sortArr===arr);